package controller

import (
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/controller/param"
	"src/model"
	"src/util/context"
	"src/util/log"
	"strconv"
)

var messageLogger = log.GetLogger()

func writemessageLoggerLoggerLog(funcName, errMsg string, err error) {
	messageLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage,
		"file":     "message.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func CreateMessage(c echo.Context) error {
	M := model.Message{}
	err := c.Bind(&M)
	id := context.GetJWTUserOpenid(c)
	M.ActionUserId = id
	if err != nil {
		writemessageLoggerLoggerLog("CreateMessage", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	err = model.CreatMessage(M)
	if err != nil {
		writemessageLoggerLoggerLog("CreateMessage", config.ErrorMessageCreat, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMessageCreat)
	}
	return context.RetData(c, "")
}

func GetMessage(c echo.Context) error {
	page, err := strconv.Atoi(c.QueryParam("page"))
	perpage, err := strconv.Atoi(c.QueryParam("per_page"))
	if perpage <= 0 || page <= 0 {
		writemessageLoggerLoggerLog("GetMessage", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}

	user_id := context.GetJWTUserOpenid(c)
	resData := map[string]interface{}{}

	var message []param.Message
	M := param.Message{}
	messages, err := model.GetMessage(user_id, page, perpage)
	if err != nil && len(messages) == 0 {
		writemessageLoggerLoggerLog("GetMessage", config.ErrorMessageGet, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMessageGet)
	}

	for i := 0; i < len(messages); i++ {
		M.Message = messages[i]
		if M.Message.Anonymity == false {
			M.UserInfo, err = model.GetUserInfo(messages[i].ActionUserId)
		} else {
			M.UserInfo = model.User{}
			M.Message.ActionUserId = ""
		}
		switch messages[i].Type {
		case 11:
			F, err := model.GetCommentByid(messages[i].ActionContentId, "")
			if err != nil {
				writemessageLoggerLoggerLog("GetMessage:11", config.ErrorMessageGet, err)
			}
			if len(F.Imgs) != 0 {
				M.Img.MicroURL = F.Imgs[0].MicroURL
				M.Img.URL = F.Imgs[0].URL
			}
			M.Text = F.Text
			break
		case 12:
			F, err := model.GetForumpostByid(messages[i].ContentId, "")
			if err != nil {
				writemessageLoggerLoggerLog("GetMessage:12", config.ErrorMessageGet, err)
			}
			if len(F.Imgs) != 0 {
				M.Img.MicroURL = F.Imgs[0].MicroURL
				M.Img.URL = F.Imgs[0].URL
			}
			M.Text = F.Text
			break
		case 14:
			F, err := model.GetReplyByid(messages[i].ActionContentId)
			if err != nil {
				writemessageLoggerLoggerLog("GetMessage:14", config.ErrorMessageGet, err)
			}
			if len(F.Imgs) != 0 {
				M.Img.MicroURL = F.Imgs[0].MicroURL
				M.Img.URL = F.Imgs[0].URL
			}
			M.Text = F.Text
			break
		case 15:
			F, err := model.GetCommentByid(messages[i].ContentId, "")
			if err != nil {
				writemessageLoggerLoggerLog("GetMessage:15", config.ErrorMessageGet, err)
			}
			if len(F.Imgs) != 0 {
				M.Img.MicroURL = F.Imgs[0].MicroURL
				M.Img.URL = F.Imgs[0].URL
			}
			M.Text = F.Text
			break
		case 18:
			F, err := model.GetReplyByid(messages[i].ActionContentId)
			if err != nil {
				writemessageLoggerLoggerLog("GetMessage:18", config.ErrorMessageGet, err)
			}
			if len(F.Imgs) != 0 {
				M.Img.MicroURL = F.Imgs[0].MicroURL
				M.Img.URL = F.Imgs[0].URL
			}
			M.Text = F.Text
			break
		default:
			break
		}
		message = append(message, M)
	}
	if len(message) == 0 {
		resData["Messages"] = []param.Message{}
	} else {
		resData["Messages"] = message
	}
	return context.RetData(c, resData)
}

func ReadMessage(c echo.Context) error {
	user_id := context.GetJWTUserOpenid(c)
	_ = model.ReadMessage(user_id)
	return context.RetData(c, "")
}
